package com.bestcem.xm.user.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.common.core.enums.ResourceErrorCodeEnum;
import com.bestcem.xm.user.dao.UserPrivateKeyDao;
import com.bestcem.xm.user.entity.pojo.UserPrivateKeyDO;
import com.bestcem.xm.user.service.UserPrivateKeyService;
import com.bestcem.xm.user.service.dto.user.UserPrivateKeyDTO;
import com.bestcem.xm.user.util.convert.UserPrivateKeyConvert;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 公钥私钥基础服务
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/24 15:04
 */
@Service
public class UserPrivateKeyServiceImpl implements UserPrivateKeyService {

    @Resource
    private UserPrivateKeyDao userPrivateKeyDao;

    @Resource
    private UserPrivateKeyConvert userPrivateKeyConvert;

    @Override
    public ServiceResult<UserPrivateKeyDTO> selectById(String privateKeyId) {
        if (StringUtils.isBlank(privateKeyId)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "公钥私钥记录id不能为空");
        }
        UserPrivateKeyDO userPrivateKeyDO = userPrivateKeyDao.selectById(privateKeyId);
        if (Objects.isNull(userPrivateKeyDO)) {
            return ServiceResult.fail(ResourceErrorCodeEnum.NOT_FOUND, "未查询到对应的公钥私钥信息");
        }
        return ServiceResult.success(userPrivateKeyConvert.do2Dto(userPrivateKeyDO));
    }

    @Override
    public ServiceResult<String> insertSelective(UserPrivateKeyDTO userPrivateKeyDTO) {
        if (Objects.isNull(userPrivateKeyDTO)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "参数信息不能为空");
        }
        String privateKeyId = userPrivateKeyDao.insertSelective(userPrivateKeyConvert.dto2Do(userPrivateKeyDTO));
        return ServiceResult.success(privateKeyId);
    }

    @Override
    public ServiceResult<Void> deleteById(String privateKeyId) {
        if (StringUtils.isNotBlank(privateKeyId)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "公钥私钥记录不能为空");
        }
        userPrivateKeyDao.deleteById(privateKeyId);
        return ServiceResult.success();
    }

}
